#!/usr/bin/env python
from pygame import *
import random
from math import cos, sin

class PartBlood:
    def __init__(self, x, y, il):
        ##image
        self.image = il.blood.copy().convert()
        
        ##motion
        self.x = x
        self.y = y
        self.direction = random.randint(0, 360)
        self.speed = random.randint(0, 20)
        self.friction = 0.1
        self.gravity = 0
        
        ##life
        self.lifetime = 1 #in seconden
        self.prev_lifetime = 0
        self.life = 0
        self.image.set_colorkey((255, 255, 255))
        self.vspeed = -sin(self.direction) * self.speed
        self.hspeed = cos(self.direction) * self.speed

        
    def update(self, level, fps):
        self.vspeed += self.gravity
        self.vspeed /= self.friction + 1
        self.hspeed /= self.friction + 1
        self.x += self.hspeed
        self.y += self.vspeed
        if self.life > self.lifetime * fps:
            level.remove_part(self)
        else:
            self.life += 1
        self.prev_lifetime = self.lifetime * fps
            
    def draw(self, camera):
        self.image.set_alpha((1-float(self.life)/(self.prev_lifetime+1)) * 255)
        camera.blit(self.image, (self.x, self.y))